
/**
 * 房间红包是否过期
 * 时间一律以b站服务端时间为准，需要根据他的时间推测红包是否过期
 */
const isRedPocketExpired = (room) => {
    const localServerFetchTime = room.fetchTime;
    const redPocketInfo = room.data.popularity_red_pocket;

    // 在查询时刻，距离开抢的剩余时间
    const leftTimeWhileFetch = redPocketInfo?.[0].end_time - redPocketInfo?.[0].current_time;
    // 根据查询时刻，推测红包的开始时间（基于本服务器时间）
    const localServerStartTime = leftTimeWhileFetch + localServerFetchTime;

    const localServerCurrentTime = Math.floor(new Date().valueOf() / 1000);

    const isExpired = localServerStartTime < localServerCurrentTime;

    return isExpired;
}

/**
 * 房间天选礼物是否过期
 */
const isAnchorExpired = (room) => {
    const localServerFetchTime = room.fetchTime;
    const pocketInfo = room.data.anchor;

    // 在查询时刻，距离开抢的剩余时间
    const leftTimeWhileFetch = pocketInfo.time;
    // 根据查询时刻，推测红包的开始时间（基于本服务器时间）
    const localServerStartTime = leftTimeWhileFetch + localServerFetchTime;

    const localServerCurrentTime = Math.floor(new Date().valueOf() / 1000);

    const isExpired = localServerStartTime < localServerCurrentTime;

    return isExpired;
}

module.exports = {
    isRedPocketExpired,
    isAnchorExpired,
}